<template>
  <div class="dashboard-container">
    <!-- 顶部功能区 -->
    <div class="top-bar">
      <div class="button-group">
        <el-button
          v-for="btn in actions"
          :key="btn.name"
          :icon="btn.icon"
          class="action-btn"
          @click="setActive(btn)"
          :class="{active: activeBtn === btn.name}"
        >
          {{ btn.name }}
        </el-button>
      </div>
      <div class="sms-info">
        <span class="sms-label">短信可用余量：{{ smsBalance }}条</span>
        <el-button class="sms-btn" size="mini">购买短信</el-button>
      </div>
    </div>

    <!-- 中部核心数据区 -->
    <div class="main-data">
      <!-- 今日费用统计 -->
      <el-card class="chart-card">
        <div class="chart-header">
          <h3>今日费用统计（元）</h3>
          <div class="stats">
            <div class="stat-item electric">电费：{{ todayStats.electric }}</div>
            <div class="stat-item water">水费：{{ todayStats.water }}</div>
            <div class="stat-item debt">累计欠费：{{ todayStats.debt }}</div>
          </div>
        </div>
        <div id="todayChart" class="chart-container"></div>
      </el-card>
      <el-card class="chart-card">
        <div class="chart-header">
          <h3>近12个月水电费用趋势</h3>
        </div>
        <div id="annualChart" class="chart-container"></div>
      </el-card>
    </div>

    <!-- 底部设备监控区 -->
    <div class="bottom-monitor">
      <!-- 设备状态 -->
      <el-card class="device-card">
        <div class="device-status">
<!--          <div class="stat-title">在线率 {{ (devices.electric.online/devices.electric.total*100).toFixed(1) }}%</div>
          <p>
    <span>
      <span>在线设备</span>
      <span class="online-dot"></span>
    </span>
            {{ devices.electric.online }}
          </p>-->
          <div class="gauge-item">
            <h3>电表状态</h3>
            <div id="electricGauge" class="gauge"></div>
            <div class="device-stats">
              <p>总数：{{ devices.electric.total }}</p>
              <p>在线：{{ devices.electric.online }} <span class="online-dot"></span></p>
              <p>失联：{{ devices.electric.offline }} <span class="offline-dot"></span></p>
            </div>
          </div>
          <div class="gauge-item">
            <h3>水表状态</h3>
            <div id="waterGauge" class="gauge"></div>
            <div class="device-stats">
              <p>总数：{{ devices.water.total }}</p>
              <p>在线：{{ devices.water.online }} <span class="online-dot"></span></p>
              <p>失联：{{ devices.water.offline }} <span class="offline-dot"></span></p>
            </div>
          </div>
        </div>
      </el-card>
<!--      <div class="slide-switch" :class="{ active: isSlideActive }" @click="toggleSlide">
        <div class="slider"></div>
        <span class="label">{{ isSlideActive ? 'ON' : 'OFF' }}</span>
      </div>-->
      <!-- 实时看板 -->
      <el-card class="realtime-board">
        <div class="carousel-control">
          <el-button
            size="mini"
            icon="el-icon-arrow-right"
            @click="nextSlide"
            class="switch-btn"
          >下一项</el-button>
        </div>
        <el-carousel ref="carousel" :interval="10000" height="200px" arrow="never">
          <el-carousel-item v-for="(item, index) in realtimeData" :key="index">
            <div class="board-item" :class="{warn: item.isWarn}">
              <h4>{{ item.title }}</h4>
              <p class="value">{{ item.value }}</p>
              <p class="time">更新时间：{{ item.time }}</p>
            </div>
          </el-carousel-item>
        </el-carousel>
      </el-card>
<!--      <el-card class="realtime-board">
        <el-carousel :interval="10000" height="200px" arrow="never">
          <el-carousel-item v-for="(item, index) in realtimeData" :key="index">
            <div class="board-item" :class="{warn: item.isWarn}">
              <h4>{{ item.title }}</h4>
              <p class="value">{{ item.value }}</p>
              <p class="time">更新时间：{{ item.time }}</p>
            </div>
          </el-carousel-item>
        </el-carousel>
      </el-card>-->
    </div>
  </div>
</template>

<script>
import * as echarts from 'echarts'

export default {
  data() {
    return {
      isSlideActive: false,
      activeBtn: '充值缴费',
      smsBalance: 1000,
      actions: [
        { name: '充值缴费', icon: 'el-icon-wallet' },
        { name: '一键开户', icon: 'el-icon-user' },
        { name: '账单查询', icon: 'el-icon-document' },
        { name: '设备阈控', icon: 'el-icon-setting' },
        { name: '费用报表', icon: 'el-icon-data-line' },
        { name: '保电设置', icon: 'el-icon-lock' }
      ],
      todayStats: {
        electric: 3000,
        water: 14300,
        debt: 12349
      },
      devices: {
        electric: { total: 130, online: 120, offline: 10 },
        water: { total: 130, online: 130, offline: 0 }
      },
      months: ['1月', '2月', '3月', '4月', '5月', '6月', '7月', '8月', '9月', '10月', '11月', '12月'],
      realtimeData: [
        { title: '当前功率', value: '356 kW', time: '10:23:45', isWarn: false },
        { title: '今日用水量', value: '156 吨', time: '10:23:45', isWarn: true },
        { title: '人均能耗排名', value: '3号楼第一', time: '10:23:45', isWarn: false }
      ]
    }
  },
  mounted() {
    this.initTodayChart()
    this.initAnnualChart()
    this.initGauges()
  },
  methods: {
    nextSlide() {
      this.$refs.carousel.next()
    },
    toggleSlide() {
      this.isSlideActive = !this.isSlideActive
      // 这里可以添加开关触发后的操作
      if (this.isSlideActive) {
        this.$message.success('滑动开关已激活')
      } else {
        this.$message.warning('滑动开关已关闭')
      }
    },
    initTodayChart() {
      const chart = echarts.init(document.getElementById('todayChart'))
      chart.setOption({
        tooltip: { trigger: 'axis' },
        xAxis: { data: ['00:00', '04:00', '08:00', '12:00', '16:00', '20:00'] },
        yAxis: { type: 'value' },
        series: [
          { name: '电费', type: 'bar', data: [300, 450, 600, 800, 1200, 3000], color: '#F56C6C' },
          { name: '水费', type: 'bar', data: [500, 800, 1200, 1800, 2400, 14300], color: '#409EFF' }
        ]
      })
    },
    initAnnualChart() {
      const chart = echarts.init(document.getElementById('annualChart'))
      chart.setOption({
        tooltip: { trigger: 'axis' },
        xAxis: { data: this.months },
        yAxis: { type: 'value' },
        series: [
          { name: '水费', type: 'line', data: [1200, 1400, 1000, 800, 900, 1100, 1300, 1500, 14300, 12000, 9500, 8000], color: '#409EFF' },
          { name: '电费', type: 'line', data: [800, 900, 1200, 1500, 1800, 2000, 2200, 2500, 3000, 2800, 2600, 2400], color: '#F56C6C' }
        ]
      })
    },
    initGauges() {
      this.initGauge('electricGauge', this.devices.electric.online / this.devices.electric.total * 100, '#67C23A')
      this.initGauge('waterGauge', this.devices.water.online / this.devices.water.total * 100, '#409EFF')
    },
    initGauge(chartId, value, color) {
      const chart = echarts.init(document.getElementById(chartId))
      chart.setOption({
        series: [{
          type: 'gauge',
          progress: { show: true },
          axisLine: { lineStyle: { color: [[1, color]] } },
          pointer: { show: false },
          detail: { show: false },
          data: [{ value }]
        }]
      })
    },
    setActive(btn) {
      this.activeBtn = btn.name
    },
    // 在updateHeatmap方法中检查配置
    updateHeatmap() {
      this.heatmapChart.setOption({
        // 确保xAxis和yAxis数据正确
        xAxis: {
          data: Array.from({length:24}, (_,i)=> `${i}时`) // 24小时标签
        },
        yAxis: {
          data: ['周一','周二','周三','周四','周五','周六','周日'] // 7天标签
        },
        series: [{
          data: this.heatmapData // 确保使用生成的数据
        }]
      })
    },
    // 在generateMockData方法中检查数据生成
    generateMockData() {
      // 热力图数据生成
      this.heatmapData = []
      for (let i = 0; i < 7; i++) {
        for (let j = 0; j < 24; j++) {
          this.heatmapData.push([j, i, Math.floor(Math.random() * 10)]) // 确保有值
        }
      }
    
      // 排名数据生成
      this.rankingData = [
        { name: '101', value: 65 },
        // ...其他数据
      ]
    }
  }
}
</script>

<style scoped>
.carousel-control {
  position: absolute;
  right: 20px;
  top: 20px;
  z-index: 1000;
}

.switch-btn {
  padding: 7px 12px;
  background: rgba(255,255,255,0.9);
  border-color: #ebeef5;
  box-shadow: 0 2px 4px rgba(0,0,0,0.1);
}
.dashboard-container {
  height: 100vh;
  display: flex;
  flex-direction: column;
  overflow: hidden;
}

.top-bar {
  flex-shrink: 0;
  height: auto;
  min-height: 80px;
  padding: 10px 20px;
  display: flex;
  justify-content: space-between;
  align-items: center;
  background: white;
  box-shadow: 0 2px 12px rgba(0,0,0,0.1);
}

.button-group {
  flex: 1;
  display: flex;
  gap: 15px;
}

.action-btn {
  padding: 12px 20px;
  transition: all 0.3s;
  &:hover {
    background: #ecf5ff;
    transform: translateY(-2px);
  }
  &.active {
    border-color: #67C23A;
    border-width: 2px;
  }
}

.sms-info {
  display: flex;
  align-items: center;
  gap: 15px;
  .sms-label {
    color: #909399;
    font-size: 14px;
  }
  .sms-btn {
    background: #f5f7fa;
    border-color: #ebeef5;
  }
}

.main-data {
  flex: 1;
  display: flex;
  flex-direction: column;
  overflow: auto;
  padding: 15px;
  height: 60vh;
  gap: 20px;
  .chart-card {
    margin-bottom: 20px;
    flex: none;
    .chart-container {
      height: 300px;
      min-height: 300px;
    }
  }
}

.chart-header {
  display: flex;
  justify-content: space-between;
  margin-bottom: 20px;
  .stats {
    display: flex;
    gap: 30px;
    .stat-item {
      padding: 8px 15px;
      border-radius: 4px;
      &.electric { background: #fef0f0; color: #f56c6c; }
      &.water { background: #ecf5ff; color: #409eff; }
      &.debt { background: #fdf6ec; color: #e6a23c; }
    }
  }
}

.bottom-monitor {
  flex-shrink: 0;
  height: auto;
  min-height: 250px;
  padding: 15px;
  overflow: auto;
  display: flex;
  gap: 20px;
  .device-card, .realtime-board {
    height: 100%;
  }
}

.device-status {
  display: flex;
  flex-wrap: wrap;
  gap: 20px;
  grid-template-columns: 1fr 1fr;

  .gauge-item {
    flex: 1;
    min-width: 300px;
    display: flex;
    flex-direction: column;
    align-items: center;

    h3 {
      margin-bottom: 15px;
      font-size: 16px;
      color: #303133;
    }

    .device-stats {
      margin-top: 15px;
      width: 100%;
      p {
        margin: 12px 0;
        font-size: 14px;
        display: flex;
        justify-content: space-between;
        align-items: center;
        padding: 8px 15px;
        background: #f8f9fa;
        border-radius: 4px;

        span {
          display: flex;
          align-items: center;
          gap: 8px;
        }
      }
    }
  }
}
@media (max-height: 800px) {
  .chart-container {
    height: 250px ;
  }

  .bottom-monitor {
    min-height: 200px;
  }
}
@keyframes blink {
  50% { background: #fef0f0; }
}
/* 新增状态说明样式 */
.device-stats {
  .stat-title {
    color: #909399;
    font-size: 12px;
    margin-bottom: 8px;
  }

  .progress-label {
    display: flex;
    justify-content: space-between;
    margin-top: 5px;
    font-size: 12px;
  }
}
.realtime-board {
  .board-item {
    padding: 20px;
    &.warn {
      animation: blink 1s infinite;
      .value { color: #f56c6c; }
    }
    .value {
      font-size: 32px;
      margin: 15px 0;
    }
    .time {
      color: #909399;
      font-size: 12px;
    }
  }
}
.slide-switch {
  position: fixed;
  right: 30px;
  bottom: 30px;
  width: 100px;
  height: 40px;
  background: #dcdfe6;
  border-radius: 20px;
  cursor: pointer;
  transition: all 0.3s;
  display: flex;
  align-items: center;
  padding: 0 10px;
  z-index: 999;
}

.slide-switch.active {
  background: #409EFF;
}

.slider {
  width: 30px;
  height: 30px;
  background: #fff;
  border-radius: 50%;
  transform: translateX(0);
  transition: all 0.3s;
}

.slide-switch.active .slider {
  transform: translateX(50px);
}

.label {
  position: absolute;
  right: 15px;
  color: #fff;
  font-size: 14px;
  user-select: none;
}

.slide-switch:not(.active) .label {
  left: 15px;
  color: #909399;
}
@keyframes blink {
  50% { background: #fef0f0; }
}
</style>
